package jp.ac.kobe_u.cs.cream;

import jp.ac.kobe_u.cs.cream.*;
import java.util.*;

public class Sequential extends Constraint {
    private Variable v[];
    private int l[];

    public Sequential(Network net, Variable v[], int l[]) {
	super(net);
	this.v = (Variable[])v.clone();
	this.l = (int[])l.clone();
    }

    protected Constraint copy(Network net) {
	return new Sequential(net, Constraint.copy(v, net), l);
    }

    protected boolean isModified() {
	return isModified(v);
    }

    protected boolean satisfy(Trail trail) {
	for (int i = 0; i < v.length - 1; i++) {
	    int j = i + 1;
	    IntDomain d0 = (IntDomain)v[i].getDomain();
	    IntDomain d1 = (IntDomain)v[j].getDomain();
	    int diffMin = d1.max() - l[i] + 1;
	    int diffMax = d0.min() + l[i] - 1;
	    d0 = d0.delete(diffMin, IntDomain.MAX_VALUE);
	    if (d0.isEmpty())
		return false;
	    d1 = d1.delete(IntDomain.MIN_VALUE, diffMax);
	    if (d1.isEmpty())
		return false;
	    v[i].updateDomain(d0, trail);
	    v[j].updateDomain(d1, trail);
	}
	return true;
    }

    public String toString() {
	return "Sequential(" + Constraint.toString(v) + "," 
	    + Constraint.toString(l) + ")";
    }
}

